package cn.lxg.service.impl;

import cn.lxg.dao.INovelChapterDao;
import cn.lxg.dao.INovelDao;
import cn.lxg.dao.IUserBookrackDao;
import cn.lxg.pojo.TNovel;
import cn.lxg.service.INovelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class NovelServiceImpl implements INovelService {

    @Autowired
    private final INovelDao novelDao = null;

    @Autowired
    private final INovelChapterDao novelChapterDao = null;

    @Autowired
    private final IUserBookrackDao userBookrackDao = null;

    @Override
    @Transactional
    public int countDataLine(TNovel novel) {
        return this.novelDao.countDataLine(novel);
    }

    @Override
    @Transactional
    public List<TNovel> getList(Integer limit, Integer page, TNovel novel) {
        /* 计算页面 */
        Integer limitStart = null;
        Integer limitEnd = null;
        if (limit != null && page != null) {
            limitStart = (page - 1) * limit;
            limitEnd = limit;
        }
        return this.novelDao.select(limitStart, limitEnd, novel);
    }

    @Override
    @Transactional
    public List<TNovel> getList1(Integer limit, Integer page, TNovel novel) {
        /* 计算页面 */
        Integer limitStart = null;
        Integer limitEnd = null;
        if (limit != null && page != null) {
            limitStart = (page - 1) * limit;
            limitEnd = limit;
        }
        return this.novelDao.select1(limitStart, limitEnd, novel);
    }

    @Override
    @Transactional
    public TNovel get(Integer id) {
        return this.novelDao.findById(id);
    }

    @Override
    @Transactional
    public String add(TNovel novel) {
        novel.setUpdateTime(new Date());
        novel.setCreateTime(new Date());
        if (this.novelDao.insert(novel))
            return "创建文章成功";
        else
            return "创建文章失败";
    }

    @Override
    @Transactional
    public String delete(Integer id) {
        this.novelChapterDao.deleteByNovelId(id);
        this.userBookrackDao.deleteByNovelId(id);
        if (this.novelDao.deleteById(id))
            return "删除文章成功";
        else
            return "删除文章失败";
    }

    @Override
    @Transactional
    public String edit(TNovel novel) {
        // 查旧文章
        TNovel oldNovel = this.novelDao.findById(novel.getId());

        // 更新属性
        oldNovel.setNovelGroupId(novel.getNovelGroupId());
        oldNovel.setName(novel.getName());
        oldNovel.setSynopsis(novel.getSynopsis());
        oldNovel.setStatus(novel.getStatus());
        oldNovel.setUpdateTime(new Date());

        // 保存数据
        if (this.novelDao.updateById(oldNovel))
            return "编辑文章成功";
        else
            return "编辑文章失败";
    }

    @Override
    public int countDataLine1(TNovel novel)  {
        return this.novelDao.countDataLine1(novel);
    }

    @Override
    public List<TNovel> getList2(Integer limit, Integer page, TNovel novel) {
        /* 计算页面 */
        Integer limitStart = null;
        Integer limitEnd = null;
        if (limit != null && page != null) {
            limitStart = (page - 1) * limit;
            limitEnd = limit;
        }
        return this.novelDao.select2(limitStart, limitEnd, novel);
    }

    @Override
    public List<TNovel> getList0(Integer limit, Integer page, TNovel novel) {
        /* 计算页面 */
        Integer limitStart = null;
        Integer limitEnd = null;
        if (limit != null && page != null) {
            limitStart = (page - 1) * limit;
            limitEnd = limit;
        }
        return this.novelDao.select0(limitStart, limitEnd, novel);
    }

}
